<script src="../../../config/index.js"></script>
<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
</style>
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="listQuery.startTime"
        align="right"
        type="datetime"
        placeholder="开始日期"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      -
      <el-date-picker
        v-model="listQuery.endTime"
        align="right"
        type="datetime"
        placeholder="结束日期"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <el-select v-model="listQuery.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" @click="getlistQuery">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              @row-click="detail" :header-cell-style="{background:'#92D3FF',color:'#fff'}">
      <!--<el-table-column fixed align="center" min-width="100" label="表单ID" prop="id"/>-->
      <el-table-column align="center" min-width="100" label="表单编号" prop="formNo"/>
      <el-table-column align="center" min-width="120" label="保险公司名称" prop="suranceName"/>
      <el-table-column align="center" min-width="120" label="保代公司名称" prop="agentName"/>
      <el-table-column align="center" min-width="120" label="保理公司名称" prop="factoringName"/>
      <el-table-column align="center" min-width="100" label="上传时间" prop="addTime"/>
      <el-table-column align="center" min-width="110" label="关联类型" prop="type">
        <template slot-scope="scope"><el-tag>{{ scope.row.type == 0 ? '保代公司' : '权益服务商' }}</el-tag></template>
      </el-table-column>
      <!--<el-table-column align="center" min-width="100" label="保单总数" prop="billCount"/>-->
      <el-table-column align="center" min-width="120" label="费用总额 (元)" prop="amount"/>
      <el-table-column align="center" min-width="100" label="表单状态" prop="status">
        <template slot-scope="scope">{{ scope.row.status | statesFilter }}</template>
      </el-table-column>
      <el-table-column align="center" min-width="220" label="保代公司/权益服务商处理时间" prop="advanceTime"/>
      <el-table-column align="center" min-width="130" label="需要垫资保单数" prop="advanceCount"/>
      <el-table-column align="center" min-width="140" label="需要垫资金额 (元)" prop="advanceAmount"/>
      <el-table-column align="center" min-width="120" label="垫资支付时间" prop="advancePayTime"/>
      <el-table-column align="center" min-width="130" label="付款总额 (元)" prop="repayAmount"/>
      <el-table-column align="center" min-width="100" label="付款时间" prop="repayTime"/>
      <el-table-column align="center" min-width="100" label="上传备注" prop="remarkUp"/>
      <el-table-column align="center" min-width="120" label="垫资申请备注" prop="remarkAdv"/>
      <el-table-column align="center" min-width="100" label="垫资利息" prop="interest"/>
      <el-table-column align="center" min-width="120" label="利息付款时间" prop="interestTime"/>
      <el-table-column fixed="right" align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click.stop="rmb(scope.row)" v-if="scope.row.status==1">
            垫资确认
          </el-button>
          <el-button type="danger" size="mini" @click.stop="goRmb(scope.row)" v-if="scope.row.status==2">
            打款确认
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
    <el-dialog :visible.sync="editDialogVisible" title="表单明细" :close-on-click-modal="false">
      <el-table v-loading="listLoading1" :data="list1" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">
        <el-table-column align="center" min-width="100" label="保单号或合同号" prop="billNo"/>
        <el-table-column align="center" min-width="100" label="保单金额" prop="billAmount"/>
        <el-table-column align="center" min-width="100" label="代理手续费/服务费" prop="fee"/>
        <el-table-column align="center" min-width="100" label="是否需要垫资" prop="isAdvance">
          <template slot-scope="scope">{{ scope.row.isAdvance | typeFilter }}</template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editDialogVisible = false">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="editDialogVisible1" title="垫资打款确认" :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <!--<el-form-item label="表单ID" prop="id">
          <el-input v-model="editForm.id" auto-complete="off" disabled/>
        </el-form-item>-->
        <el-form-item label="垫资打款金额" prop="payAmount">
          <el-input v-model="editForm.payAmount" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="打款日期" prop="repayDate">
          <el-date-picker
            v-model="editForm.repayDate"
            align="right"
            type="datetime"
            placeholder="打款日期"
            value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="doRmb">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {doGet, doPost} from '@/api'
  import Pagination from '@/components/Pagination'

  const FETCH_PAGE = 'blagentfee.html'
  const FETCH_URL = {
    LIST: `${FETCH_PAGE}?op=list`,
    CREATE: `${FETCH_PAGE}?op=billlist`,
    EDIT: `${FETCH_PAGE}?op=check`,
    DELETE: `${FETCH_PAGE}?op=pay`,
  }
  export default {
    name: 'blagentfee',
    components: {Pagination},
    filters: {
      statesFilter(type) {
        return {
          '0': '',
          '1': '垫资已申请',
          '2': '保理公司已接收',
          '3': '保理公司垫资已支付',
          '4': '保险公司已回款',
          '5': '保代公司已确认',
          '6': '垫资利息已支付',
          '7': '权益服务商已取款',
        }[type]
      },
      typeFilter(type) {
        return {
          '0': '待确认',
          '1': '不需要',
          '2': '需要',
        }[type]
      }
    },
    data() {
      return {
          total: 0,
        options: [{
          value: '',
          label: '请选择状态'
        }, {
          value: '1',
          label: '垫资已申请'
        }, {
          value: '2',
          label: '保理公司已接收'
        }, {
          value: '3',
          label: '保理公司垫资已支付'
        }, {
          value: '4',
          label: '保险公司已回款'
        }, {
          value: '5',
          label: '保代公司已确认'
        }, {
          value: '6',
          label: '垫资利息已支付'
        },{
            value: '7',
            label: '权益服务商已取款'
        }],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        list: [],
        list1: [],
        listLoading: true,
        listLoading1: true,
        listQuery: {
          startTime: undefined,
          endTime: undefined,
          status: undefined,
            pageNum: 1,
            pageSize: 20
        },
        editDialogVisible: false,
        editDialogVisible1: false,
        editForm: {
          id: undefined,
          formType: undefined,
          payAmount: undefined,
          repayDate: undefined,
        },
        editFormRules: {
          id: [{required: true, message: '表单ID不允许为空', trigger: 'blur'}],
          payAmount: [{required: true, message: '垫资打款金额不允许为空，并与申请垫资额一致', trigger: 'blur'}],
          repayDate: [{required: true, message: '打款日期不允许为空', trigger: 'blur'}],
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        doGet(FETCH_URL.LIST, this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.data.totalcount
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      detail(row) {
        this.editDialogVisible = true
        this.listLoading1 = true
//        console.log(this.list)
          this.editForm1 = {
              id: row.id,
              formType: row.type
          }
        doGet(FETCH_URL.CREATE, this.editForm1 ).then(response => {
          console.log(response)
          this.list1 = response.rows
          this.listLoading1 = false
        }).catch(() => {
//          this.total = 0
          this.listLoading1 = false
        })
      },
      getlistQuery() {
        this.getList()
      },
      rmb(row) {
          this.editForm2 = {
              id: row.id,
              formType: row.type
          }
        doGet(FETCH_URL.EDIT, this.editForm2).then(response => {
          console.log(response)
            this.getList()
            this.listLoading1 = false
            this.$notify.success({
                title: '成功',
                message: '垫资成功'
            })
        }).catch(response => {
          this.listLoading1 = false
            this.$notify.error({
                title: '失败',
                message: response.message
            })
        })
      },
      goRmb(row) {
        this.editDialogVisible1 = true
        this.editForm = {
          id: undefined,
          payAmount: undefined,
          repayDate: undefined,
          formType: undefined
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
        this.editForm.id=row.id
        this.editForm.formType=row.type
      },
      doRmb(){
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm,
            }
            doGet(FETCH_URL.DELETE, sendData)
              .then(response => {
                this.getList()
                this.editDialogVisible1 = false
                this.$notify.success({
                  title: '成功',
                  message: '垫资打款成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
                this.editDialogVisible1 = false
              })
          }
        })
      }
    }
  }
</script>
